var root={};
root.Validator={};
root.Validator={
		form:"form-data",
	    reset:"reset"
};
root.Validator.reset=function(){
	$("#"+root.Validator.reset).on("click",function(){
        if(validator!==undefined) {
            validator.resetForm();
        };
    });
};
root.Validator.resetValidator=function(validator){
	if(validator!==undefined){
		validator.resetForm();
	}
};
root.Validator.User={};
root.Validator.User.validate = function(){
	var formContainer,validator;
    if(arguments.length==0){
        formContainer = $("#"+root.Validator.form);
    }
    validator = formContainer.validate({
        rules:{
            username:"required",
            password:"required",
            realname:"required",
            roleIds:"required"
        },
        messages:{
            username:"请输入用户名",
            password:"请输入密码",
            realname:"请填写真实名",
            roleIds:"请选择角色"
        }
    });
    root.Validator.resetValidator(validator);
};

root.Validator.initData=function(options){
	var formContainer,validator;
    if(options===undefined || options.container===undefined){
        formContainer = $("#"+root.Validator.form);
    }else{
    	formContainer = $(container);
    }
    validator = formContainer.validate({
        rules:options.rules,
        messages:options.messages
    });
    root.Validator.resetValidator(validator);
};
root.Validator.UserStation={};
root.Validator.UserStation.validate=function(){
	var options={
			rules:{
	            userId:{min:1,remote:root.Validator.UserStation.getSame()},
	            stationId:{min:1,remote:root.Validator.UserStation.getSame()}
	        },
	        messages:{
	        	userId:{min:'请选择用户',remote:'已经存在，请重新选择'},
	            stationId:{min:'请选择工位',remote:'已经存在，请重新选择'}
	        }
	};
    root.Validator.initData(options);
};
root.Validator.UserStation.getSame=function(){
	var userId=$("#userId option:selected").val(),
	    stationId=$("#stationId:selected").val();
	$.get("findSame.jspx",{userId:userId,stationId:stationId,format:'json'},function(data){
		return data;
	});
};

root.Validator.Role={};
root.Validator.Role.validate=function(){
	var options={
			rules:{
	            name:"required",
	            identity:"required",
	            index:"required",
	            resourceIds:"required"
	        },
	        messages:{
	        	name:"请填写名称",
	            identity:"请填写标识",
	            index:"请填写首页地址，可默认about.jspx",
	            resourceIds:"请选择资源"
	        }
	};
	root.Validator.initData(options);
};

root.Validator.Material={};
root.Validator.Material.validate=function(){
	var options={
			rules:{
				code:{required:true,remote:"check.jspx?format=json"},
				category:{min:1},
				unit:"required"
			}
	       ,messages:{
				code:{required:"请填写编号",remote:"编号已经存在，请重新填写"},
				category:"请选择物料类别",
				unit:"请填写单位"
	       }
	};
	root.Validator.initData(options);
};
root.Validator.Material.editValidate=function(){
	var options={
			rules:{
				name:"required",
				category:{min:1},
				unit:"required"
			}
	       ,messages:{
	    	   name:"请填写物料名称",
				category:"请选择物料类别",
				unit:"请选择单位"
	       }
	};
	root.Validator.initData(options);
};

root.Validator.Bom={};
root.Validator.Bom.validate=function(){
	var options={
			rules:{
				code:"required"
			}
	       ,messages:{
	    	   code:"请选择主物料编号"
	       }
	};
	root.Validator.initData(options);
};

root.Validator.Plan={};
root.Validator.Plan.validate=function(){
	var options={
			rules:{
				name:"required"
			}
	       ,messages:{
	    	   name:"请填写计划名称"
	       }
	};
	root.Validator.initData(options);
};

root.Validator.StockIn={};
root.Validator.StockIn.validate=function(){
	var options={
			rules:{
				materialCode:"required",
				totalNum:"required"
			}
	       ,messages:{
	    	   materialCode:"请填写物料编号",
				totalNum:"请填写所需物料"
	       }
	};
	root.Validator.initData(options);
};

root.Validator.StockOut={};
root.Validator.StockOut.validate=function(){
	var options={
			rules:{
				materialCode:"required",
				totalNum:"required"
			}
	       ,messages:{
	    	   materialCode:"请填写物料编号",
				totalNum:"请填写所需物料"
	       }
	};
	root.Validator.initData(options);
};

root.Validator.StockReturn={};
root.Validator.StockReturn.validate=function(){
	var options={
			rules:{
				materialCode:"required",
				totalNum:"required"
			}
	       ,messages:{
	    	   materialCode:"请填写物料编号",
				totalNum:"请填写所需物料"
	       }
	};
	root.Validator.initData(options);
};